package com.czbank.financing.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czbank.financing.Annotation.PassToken;
import com.czbank.financing.Entity.Account;
import com.czbank.financing.Entity.UserInfo;
import com.czbank.financing.service.LoginService;
import com.czbank.financing.service.UserInfoService;
import com.czbank.financing.utils.ReturnInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import sun.security.util.Password;

import java.util.Random;

/**
 * @Auther: thinkpad
 * @Date: 2019/7/12 18:03 s
 * @Description:swwww
 */
@CrossOrigin
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;



    @RequestMapping(value = "/SelectOneUserInfo",method = RequestMethod.POST)
    public Object UserInfoOne(@RequestBody JSONObject  jsonObject){

        String username = jsonObject.getString("username");
        System.out.println("username = " + username);
        return JSON.toJSONString(userInfoService.SelectOneUserInfo(username));
    }

    @RequestMapping("/queryUserInfo")
    public Object QueryOneUserInfo(@RequestBody JSONObject  jsonObject){
        //System.out.println("jsonObject1 = " + jsonObject);
        String username = jsonObject.getString("username");
        System.out.println("username = " + username);
        return JSON.toJSONString(userInfoService.QueryOneUserInfo(username));
    }



    @RequestMapping("/allUserInfo")
    public Object TotalProduct(){
        //System.out.println("jsonObject1 = " + jsonObject);
        return JSON.toJSONString(userInfoService.AllUserInfo());
    }



    @RequestMapping("/UpdateUserInfo")
    public Object UpdatUserInfo(int id, String password){
        ReturnInfo returnInfo=new ReturnInfo();

        if (userInfoService.UpdateUserInfo(id, password)==0){
            returnInfo.setRet(0);
            returnInfo.setRetinfo("失败");

        }
        else {
            returnInfo.setRet(1);
            returnInfo.setRetinfo("成功");
        }
        return returnInfo;
    }

    @RequestMapping("/InsertUserInfo")
    public Object InsertUserInfo(String account,String username,String password){
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        ReturnInfo returnInfo=new ReturnInfo();
        System.out.println("md5Password = " + md5Password);
        if (userInfoService.InsertUserInfo(account, username, md5Password)==0){
            returnInfo.setRet(0);
            returnInfo.setRetinfo("失败");

        }
        else {
            returnInfo.setRet(1);
            returnInfo.setRetinfo("成功");
        }
        return returnInfo;
    }
    @RequestMapping("/DeleteUserInfo")
    public Object DeleteUserInfo(@RequestBody JSONObject  jsonObject){
        System.out.println("DeleteUserInfo = " + jsonObject);
//        if(userInfoService.DeleteUserInfo(id)==0){
////
////            return "fail";
////        }
////        else {
////            return "success";
////        }
        return "success";
    }


    @RequestMapping("/InsertUser")
    public Object InsertUserInfoasda(){
        char[] ch = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                'j', 'k','l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        String account =new String();
        String username = new String();
        String str =new String();
        for (int j = 0; j <10 ; j++) {
        Random random = new Random();
        for (int i = 0; i <6; i++){
            char num = ch[random.nextInt(ch.length)];
            str += num;
        }

        String password ="123456";
        username = str;
        account = "13212";
            str="";
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //ReturnInfo returnInfo=new ReturnInfo();
        System.out.println("md5Password = " + md5Password);
        userInfoService.InsertUserInfo(account, username, md5Password);
        }
//        if (userInfoService.InsertUserInfo(account, username, md5Password)==0){
//            returnInfo.setRet(0);
//            returnInfo.setRetinfo("失败");
//
//        }
//        else {
//            returnInfo.setRet(1);
//            returnInfo.setRetinfo("成功");
//        }
        return "sad";
    }
}
